package com.btkjgc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.CarT;
import com.btkjgc.models.Dictionary;
import com.btkjgc.models.extend.CarModel;
import com.btkjgc.service.intf.CarListManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Component
public class CarListManageService_Impl extends BaseService_Impl implements CarListManageService_Intf
{

	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv,int pageIndex, int totalRecord, 
			List<CarModel> carModel) {
		// TODO Auto-generated method stub
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		mv.addObject("car_t", carModel);
		
		//颜色列
		String colorsql = " select dt.value,dt.key from dictionary_t dt where dt.name='CarColor'";
		
		List<Dictionary> colorList = HibernateUtil.executeSQLQueryList(colorsql, Dictionary.class);
		if(colorList.size() > 0)
		{
			mv.addObject("colorList", colorList);
		}
		
		//车型列
		String cartypesql = " select ct.name,CONCAT(id,'') 'key' from cartype_t ct where ct.del = 0";
				
		List<Dictionary> cartypeList = HibernateUtil.executeSQLQueryList(cartypesql,Dictionary.class);
		if(cartypeList.size() > 0)
		{
			mv.addObject("cartypeList", cartypeList);
		}
		
		//搜索车型列
		String searchcartype = " SELECT DISTINCT ctt.name,CONCAT(ctt.id, '') 'key' FROM cartype_t ctt LEFT JOIN car_t ct ON ct.cartypeid=ctt.id WHERE ct.del = 0";
		
		List<Dictionary> scartypeList = HibernateUtil.executeSQLQueryList(searchcartype, Dictionary.class);
		if(scartypeList.size() > 0)
		{
			mv.addObject("scartypeList", scartypeList);
		}
		
		//运营类型列
		String businesssql = " select dt.value,dt.key from dictionary_t dt where dt.name='BusinessType'";
		
		List<Dictionary> businessList = HibernateUtil.executeSQLQueryList(businesssql, Dictionary.class);
		if(businessList.size() > 0)
		{
			mv.addObject("businessList", businessList);
		}
		
		//公司列
		String companysql = " select aut.name,CONCAT(id,'') 'key' from account_user_t aut where aut.type=1 and aut.del = 0";
				
		List<Dictionary> companyList = HibernateUtil.executeSQLQueryList(companysql,Dictionary.class);
		if (companyList.size() > 0)
		{
			mv.addObject("companyList", companyList);
		}
		
		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
		
	}

	@Override
	public void addCar(CarT car) {
		// TODO Auto-generated method stub
		//如果首次免费，将isfirst置1
		switch(car.getFree()){
		
			case 1:car.setIsfirst(1);
				car.setIslast(2);
				HibernateUtil.save(car);
				break;
			case 2:car.setIsfirst(1);
				car.setIslast(0);
				HibernateUtil.save(car);
				break;
			case 3:car.setIslast(2);
				car.setIsfirst(0);
				HibernateUtil.save(car);
				break;
			case 4:car.setIsfirst(0);
				car.setIslast(0);
				HibernateUtil.save(car);
				break;
			default:
				break;
		}
	}

	@Override
	public void updateCar(CarModel carModel) {
		// TODO Auto-generated method stub
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String hql;
		
		switch(carModel.getFree()){
		
			case 1:hql = " update from CarT " +
			 	 	  " set" +
			 	 	  " platedate='"+sf.format(carModel.getPlatedate())+"',"+
			 	 	  " carplate='"+carModel.getCarplate()+"',"+
			 	 	  " framenumber='"+carModel.getFramenumber()+"',"+
			 	 	  " color="+carModel.getColor()+","+
			 	 	  " cartypeid="+carModel.getCartypeid()+","+
			 	 	  " business_type="+carModel.getBusiness_type()+","+
			 	 	  " companyid="+carModel.getCompanyid()+","+
			 	 	  " islast=2,"+
			 	 	  " isfirst=1,"+
			 	 	  " free="+carModel.getFree()+","+
			 	 	  " firstfee="+(carModel.getFirstfee()!=null? carModel.getFirstfee():0)+","+
			 	 	  " lastfee="+(carModel.getLastfee()!=null? carModel.getLastfee():0)+
			 	 	  " where id="+carModel.getId();
					HibernateUtil.executeUpdate(hql);
				break;
			case 2:hql = " update from CarT " +
			 	 	  " set" +
			 	 	  " platedate='"+sf.format(carModel.getPlatedate())+"',"+
			 	 	  " carplate='"+carModel.getCarplate()+"',"+
			 	 	  " framenumber='"+carModel.getFramenumber()+"',"+
			 	 	  " color="+carModel.getColor()+","+
			 	 	  " cartypeid="+carModel.getCartypeid()+","+
			 	 	  " business_type="+carModel.getBusiness_type()+","+
			 	 	  " companyid="+carModel.getCompanyid()+","+
			 	 	  " islast=0,"+
			 	 	  " isfirst=1,"+
			 	 	  " free="+carModel.getFree()+","+
			 	 	  " firstfee="+(carModel.getFirstfee()!=null? carModel.getFirstfee():0)+","+
			 	 	  " lastfee="+(carModel.getLastfee()!=null? carModel.getLastfee():0)+
			 	 	  " where id="+carModel.getId();
					HibernateUtil.executeUpdate(hql);
				break;
			case 3:hql = " update from CarT " +
			 	 	  " set" +
			 	 	  " platedate='"+sf.format(carModel.getPlatedate())+"',"+
			 	 	  " carplate='"+carModel.getCarplate()+"',"+
			 	 	  " framenumber='"+carModel.getFramenumber()+"',"+
			 	 	  " color="+carModel.getColor()+","+
			 	 	  " cartypeid="+carModel.getCartypeid()+","+
			 	 	  " business_type="+carModel.getBusiness_type()+","+
			 	 	  " companyid="+carModel.getCompanyid()+","+
			 	 	  " islast=2,"+
			 	 	  " isfirst=0,"+
			 	 	  " free="+carModel.getFree()+","+
			 	 	  " firstfee="+(carModel.getFirstfee()!=null? carModel.getFirstfee():0)+","+
			 	 	  " lastfee="+(carModel.getLastfee()!=null? carModel.getLastfee():0)+
			 	 	  " where id="+carModel.getId();
					HibernateUtil.executeUpdate(hql);
				break;
			case 4:hql = " update from CarT " +
			 	 	  " set" +
			 	 	  " platedate='"+sf.format(carModel.getPlatedate())+"',"+
			 	 	  " carplate='"+carModel.getCarplate()+"',"+
			 	 	  " framenumber='"+carModel.getFramenumber()+"',"+
			 	 	  " color="+carModel.getColor()+","+
			 	 	  " cartypeid="+carModel.getCartypeid()+","+
			 	 	  " business_type="+carModel.getBusiness_type()+","+
			 	 	  " companyid="+carModel.getCompanyid()+","+
			 	 	  " islast=0,"+
			 	 	  " isfirst=0,"+
			 	 	  " free="+carModel.getFree()+","+
			 	 	  " firstfee="+(carModel.getFirstfee()!=null? carModel.getFirstfee():0)+","+
			 	 	  " lastfee="+(carModel.getLastfee()!=null? carModel.getLastfee():0)+
			 	 	  " where id="+carModel.getId();
					HibernateUtil.executeUpdate(hql);
				break;
			default:
				break;
		
		}

		//同时判断car_account_t里是否存在原车牌号并做修改
		String sql = " select count(*) from car_account_t where carid="+carModel.getId();
				
		int count = HibernateUtil.executeSQLQueryInt(sql);
		if(count > 0){
			String hql2 = " update from CarAccount " +
						  " set" +
						  " carbrand='"+carModel.getCarplate()+"',"+
						  " cartypeid="+carModel.getCartypeid()+
						  " where carid="+carModel.getId();
			HibernateUtil.executeUpdate(hql2);
		}
	}

	@Override
	public void updateCarplate(CarModel carModel) {
		// TODO Auto-generated method stub
		String hql = " update from CarT " +
			 	 	 " set" +
			 	 	 " carplate='"+carModel.getCarplate()+"'"+
			 	 	 " where id="+carModel.getId();
		
		HibernateUtil.executeUpdate(hql);
		//同时判断car_account_t里是否存在原车牌号并做修改
		String sql = " select count(*) from car_account_t where carid="+carModel.getId();
		
		int count = HibernateUtil.executeSQLQueryInt(sql);
		if(count > 0){
			String hql2 = " update from CarAccount " +
						  " set" +
						  " carbrand='"+carModel.getCarplate()+"'"+
						  " where carid="+carModel.getId();
			HibernateUtil.executeUpdate(hql2);
		}
	}
	
}